package du.myseckill.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Scope;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Scope
@Component
@Aspect
//优先级为1，最先执行，并且最后结束
@Order(1)
/**
 * 对秒杀方法加ReentrantLock锁
 */
public class LockAspect {

    //互斥锁
    private static final Lock lock=new ReentrantLock(true);


    @Pointcut("execution(* du.myseckill.service.GoodsService.executeSecKillAOPLock(..))")
    public void LockPointCut(){

    }

    @Around("LockPointCut()")
    public Object LockAround(ProceedingJoinPoint joinPoint){
        lock.lock();
        System.out.println("加锁！");
        try {
            Object o = joinPoint.proceed();
            return o;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            throw new RuntimeException();
        }finally {
            lock.unlock();
            System.out.println("解锁！");
        }
    }

}
